package com.zeus.common.utils;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * 堆栈工具类
 *
 * @author wangfeixiong126@gmail.com  2021/6/23 15:04
 */
public class StackTraceUtils {

    /**
     * 获取异常堆栈字符串信息
     *
     * @param throwable 异常
     * @return 堆栈字符串信息
     * @author wangfeixiong126@gmail.com  2021/5/13 20:34
     */
    public static String getStackTrace(final Throwable throwable) {
        try {
            if (throwable == null){
                return null;
            }
            final StringWriter sw = new StringWriter();
            final PrintWriter pw = new PrintWriter(sw, true);
            throwable.printStackTrace(pw);
            return sw.getBuffer().toString();
        } catch (Throwable ignored) {
            // 忽略
        }
        return null;
    }
}
